package com.xmd.red.dao;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;

/**
 * Created by superbone on 2016/10/31.
 */
public class MorphiaUtil {

    private static Datastore datastore;

    static {
        MongoClientURI clientURI = new MongoClientURI("mongodb://redPacket:superbone@121.40.129.226/redPacket");
        MongoClient client = new MongoClient(clientURI);
        Morphia morphia = new Morphia();
        morphia.mapPackage("com.xmd");
        Datastore datastore = morphia.createDatastore(client, "redPacket");
        datastore.ensureIndexes();
    }

    public static Datastore getDataStore(){
        if(datastore==null){
            return createDateStore();
        }else{
            return datastore;
        }
    }

    private static Datastore createDateStore(){
        MongoClientURI clientURI = new MongoClientURI("mongodb://redPacket:superbone@121.40.129.226/redPacket");
        MongoClient client = new MongoClient(clientURI);
        Morphia morphia = new Morphia();
        morphia.mapPackage("com.xmd");
        Datastore datastore = morphia.createDatastore(client, "redPacket");
        datastore.ensureIndexes();
        return datastore;
    }
}
